package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    private CategoryBrandService categoryBrandService;
    @Override
    public List<BrandDTO> queryBrandByCategoryId(Long id) {
/*
        //根据分类id从中间表查询
        List<CategoryBrand> categoryBrandList = categoryBrandService.query().eq("category_id", id).list();
        //从中间表获取分类id对应商品id的集合
        List<Long> brandIds = categoryBrandList.stream()
                .map(CategoryBrand::getBrandId).collect(Collectors.toList());
        //根据品牌id 获取品牌
        if (!CollectionUtils.isEmpty(brandIds)){
            throw new LyException(400,"品牌为空");
        }
        List<Brand> brandList = listByIds(brandIds);
        //转换为dto
        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(brandList);

        return brandDTOS;*/
        List<Brand> list = getBaseMapper().queryByCategoryId(id);
        return BrandDTO.convertEntityList(list);
    }

    /**
     * 分页查询品牌
     * @param key
     * @param page
     * @param rows
     * @return
     */
    @Override
    public PageDTO<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows) {
        //分页信息的健壮性判断
        page=Math.min(page,100); //当前页最多为100页
        rows=Math.max(rows,5);   //每页显示数据为5条

        //分页
        Page<Brand> page1=new Page<>(page,rows);
        //判断查询条件是为为空或是空串
        boolean isKeyExist = StringUtils.isNoneBlank(key);
        query()
                .like(isKeyExist,"name",key)
                .or()
                .eq(isKeyExist,"letter",key)
                .page(page1);
        List<Brand> brandList = page1.getRecords();
        long total = page1.getTotal();
        long pages = page1.getPages();
        return new PageDTO<BrandDTO>(total,pages,BrandDTO.convertEntityList(brandList));
    }

    /**
     * 新增品牌
     * @param brandDTO
     */
    @Override
    public void saveBrand(BrandDTO brandDTO) {
        //将dto转为po
        Brand brand = brandDTO.toEntity(Brand.class);
        //保存
        save(brand);
        //获取保存过的brand的brangId 和brandDTO中的categoryIds
        Long brandId = brand.getId();
        List<Long> categoryIds = brandDTO.getCategoryIds();

        List<CategoryBrand> categoryBrandList = categoryIds.stream()
                .map(id -> CategoryBrand.of(id, brandId))
                .collect(Collectors.toList());
        //批量写入中间表
        categoryBrandService.saveBatch(categoryBrandList);
    }

    /**
     * 修改品牌
     * @param brandDTO
     */
    @Override
    public void updateBrand(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        //更新brand
        boolean isSuccess = updateById(brand);
        if (!isSuccess){
            throw new LyException(500,"更新品牌失败");
        }
        List<Long> categoryIds = brandDTO.getCategoryIds();
        List<CategoryBrand> categoryBrandList = categoryIds.stream()
                .map(id -> CategoryBrand.of(id, brand.getId()))
                .collect(Collectors.toList());
        //批量新增
        categoryBrandService.saveBatch(categoryBrandList);
    }


}
